@using System.Web.Optimization
@using VocaDb.Model.DataContracts.ReleaseEvents
@using VocaDb.Model.Domain.Artists
@using VocaDb.Model.Domain.Images
@using VocaDb.Model.Domain.ReleaseEvents
@using VocaDb.Model.Domain.Security
@using VocaDb.Web.Helpers;
@using Res = ViewRes.Event.DetailsStrings;
@using EntryRes = ViewRes.EntryDetailsStrings;
@inherits VocaDb.Web.Code.VocaDbPage<VocaDb.Model.DataContracts.ReleaseEvents.ReleaseEventDetailsContract>

@{

	if (Model.Series == null) {
		ViewBag.Parents = new[] {
			Html.ActionLink(ViewRes.SharedStrings.ReleaseEvents, "Index")
		};
	} else {
		ViewBag.Parents = new[] {
			Html.ActionLink(ViewRes.SharedStrings.ReleaseEvents, "Index"),
			Html.ActionLink(Model.Series.Name, "SeriesDetails", new { id = Model.Series.Id, slug = Model.Series.UrlSlug })
		};
	}

	var descriptionHtml = MarkdownParser.GetHtml(Model.Description);
	var descriptionStripped = MarkdownParser.GetPlainText(Model.Description);

	PageProperties.Description = descriptionStripped;

	var pictureData = !string.IsNullOrEmpty(Model.PictureMime) ? (IEntryImageInformation)Model: Model.Series;
	var smallThumbUrl = Url.ImageThumb(pictureData, VocaDb.Model.Domain.Images.ImageSize.SmallThumb);
	var fullImageUrl = Url.ImageThumb(pictureData, VocaDb.Model.Domain.Images.ImageSize.Original);

	var primaryPV = PVHelper.PrimaryPV(Model.PVs);

}

@section Head {
	@HtmlHelpers.OpenGraphMetaTags(PageProperties)
	<link rel="canonical" href="@PageProperties.CanonicalUrl" />
}

@section Toolbar {
	@Html.ActionLink(ViewRes.SharedStrings.Edit, "Edit", new { id = Model.Id }, new { id = "editEventLink", @class = (Login.CanEdit(Model) ? "" : "disabled") })
	@Html.ActionLink(ViewRes.EntryDetailsStrings.ViewModifications, "Versions", new { id = Model.Id }, new { id = "viewVersions" })
	<a href="#" id="reportEntryLink" data-bind="click: reportViewModel.show">@ViewRes.EntryDetailsStrings.ReportAnError</a>
	@Helpers.EntryStatusMessage(Model.Status)
}

@helper ArtistList(IList<ArtistForEventContract> artists, bool showRoles = false, bool showType = false) {
	foreach (var artist in artists) {
		if (artist.Artist != null) {
			@ArtistHelpers.ArtistLink(artist.Artist, typeLabel: showType)
		} else {
			@artist.Name
		}
		if (showRoles && artist.EffectiveRoles != ArtistEventRoles.Default) {
			@Html.Raw("&nbsp;<small class='muted'>(" + Translate.ArtistEventRoleNames.GetAllNameNames(artist.EffectiveRoles, ArtistEventRoles.Default) + ")</small>")
		}
		if (artist != artists.Last()) {
			@(", ")
		}
	}
}


@if (Model.Deleted) {
	@EntryDetailsHelpers.DeletedBanner(null)
}

<div class="well well-transparent">

	<div class="media">
		@if (pictureData != null && !string.IsNullOrEmpty(pictureData.Mime)) {
			<a class="pull-left" href="@fullImageUrl">
				<img class="media-object" src="@smallThumbUrl" alt="Thumb" />
			</a>
		}
		<div class="media-body">
			<p>
				@ViewRes.SharedStrings.Name: @Model.Name
				@if (!string.IsNullOrEmpty(Model.AdditionalNames)) {
					<small class="extraInfo">(@Model.AdditionalNames)</small>							
				}
			</p>

			@if (Model.Date != null) {
				<p>
					@Res.OccurrenceDate: @Model.Date.Value.ToShortDateString()
					@if (Model.EndDate != null && Model.EndDate.Value > Model.Date.Value) {
						@:- @Model.EndDate.Value.ToShortDateString()
					}
				</p>
			}

			@if (!string.IsNullOrEmpty(Model.VenueName)) {
				<p>@Res.Venue: @Model.VenueName</p>
			}

			@if (Model.Artists.Any()) {
				<p>Participating artists: @ArtistList(Model.Artists, true, true)</p>
			}

			<p>
				@ViewRes.Event.DetailsStrings.Category:
				@Html.ActionLink(Translate.ReleaseEventCategoryNames[Model.InheritedCategory], "Index", "Search", UrlMapper.Search.Events(category: Model.InheritedCategory), null)
			</p>

			@if (!string.IsNullOrEmpty(Model.Description)) {
				<p>@Html.Raw(descriptionHtml)</p>
			}

			@if (Model.WebLinks.Any()) {
				@EntryDetailsHelpers.ExternalLinksList(Model.WebLinks, false)
				<br/>
			}

			<p>
				<div class="inline-block">@ViewRes.SharedStrings.Tags:</div>
				<div data-bind="with: tagUsages" class="entry-tag-usages inline-block">
					@{ Html.RenderPartial("Partials/_TagList"); }
				</div>
				<a class="inline-block" data-bind="jqButton: { disabled: @ToJS(!Login.CanEditTags), icon: 'ui-icon-tag' }, click: tagsEditViewModel.show" href="#">@ViewRes.EntryDetailsStrings.EditTags</a>
				@if (Model.CanRemoveTagUsages) {
					@Html.ActionLink(ViewRes.EntryDetailsStrings.ManageTags, "ManageTagUsages", new {id = Model.Id}, new {id = "manageTags"})
				}
			</p>

		</div>
	</div>
	
</div>

@if (Model.Series != null) {
	<h3>@Res.Series: <a href="@Url.Action("SeriesDetails", new { id = Model.Series.Id, slug = Model.Series.UrlSlug })" title="@Model.Series.AdditionalNames">@Model.Series.Name</a></h3>
	if (!string.IsNullOrEmpty(Model.Series.Description)) {
		@HtmlHelpers.FormatMarkdown(Model.Series.Description);
	}
	@EntryDetailsHelpers.ExternalLinksList(Model.Series.WebLinks, false)
}

@if (Model.SongList != null) {
	<h3 class="withMargin">@Res.SongList: @Html.ActionLink(Model.SongList.Name, "Details", "SongList", new { id = Model.SongList.Id }, null)</h3>
	if (Model.SongListSongs.Any()) {
		<table class="table">
			@foreach (var song in Model.SongListSongs) {
				<tr>
					<td style="width: 50px"><h1>@song.Order</h1></td>
					<td style="width: 75px">@SongHelpers.SongIconLink(song.Song)</td>
					<td>
						@SongHelpers.SongLink(song.Song)
						<br />
						<small class="extraInfo">@song.Song.ArtistString</small>
					</td>
				</tr>
			}
		</table>
	}
}

@if (Model.Albums.Any()) {
	<h3 class="withMargin">
		@Res.Albums
		<small>@string.Format(EntryRes.NumTotalParenthesis, Model.Albums.Length)</small>
	</h3>
	@AlbumHelpers.AlbumGrid(Model.Albums, 2, false, false, true)
}

@if (Model.Songs.Any()) {
	<h3 class="withMargin">
		@Html.ActionLink(Res.Songs, "Index", "Search", UrlMapper.Search.Songs(eventId: Model.Id), null)
		<small>@string.Format(EntryRes.NumTotalParenthesis, Model.Songs.Length)</small>
	</h3>
	@SongHelpers.SongGrid(Model.Songs, 2, true, false)
}

@if (Model.PVs.Any()) {
	<h3 class="withMargin">@Res.PVs</h3>
	@Html.LinkListHtml(Model.PVs, pv => Helpers.FormatPVLink(pv, false))
}	

@if (primaryPV != null) {
	<div class="song-pv-player">
		@PVHelpers.EmbedPV(primaryPV)
	</div>
}

<h3 data-bind="visible: loggedUserId || usersAttending().length" class="withMargin">@Res.Attending</h3>

@if (Login.IsLoggedIn) {
	<div class="btn-group" data-bind="visible: hasEvent">
		<button class="btn dropdown-toggle" data-toggle="dropdown">
			<span data-bind="visible: isEventAttending">@Res.UserAttending</span>
			<span data-bind="visible: isEventInterested">@Res.UserInterested</span>
			<span class="caret"></span>
		</button>
		<ul class="dropdown-menu">
			<li><a data-bind="click: removeEvent" href="#">@Res.RemoveAttendance</a></li>
			<li><a data-bind="click: setEventAttending, visible: isEventInterested" href="#">@Res.UserAttending</a></li>
			<li><a data-bind="click: setEventInterested, visible: isEventAttending" href="#">@Res.UserInterested</a></li>
		</ul>
	</div>	
	<div class="btn-group" data-bind="visible: !hasEvent()">
		<button data-bind="click: setEventAttending" class="btn">@Res.UserAttending</button>		
		<button data-bind="click: setEventInterested" class="btn">@Res.UserInterested</button>		
	</div>
}

<div data-bind="visible: usersAttending().length" class="withMargin">
	<ul data-bind="foreach: usersAttending()">
		<li>
			@UserHelpers.IconAndNameLinkKnockout()
		</li>
	</ul>
</div>

@CommentHelpers.LatestCommentsKnockout()

<div data-bind="with: tagsEditViewModel">
	@{ Html.RenderPartial("Partials/_TagsEdit"); }
</div>

@EntryDetailsHelpers.ReportEntryPopupKnockout()

@section BodyScripts {
@Scripts.Render("~/bundles/ReleaseEvent/Details")
<script type="text/javascript">
	$(function () {
		$("#editEventLink").button({ disabled: $("#editEventLink").hasClass("disabled"), icons: { primary: 'ui-icon-wrench'} });
		$("#viewVersions").button({ icons: { primary: 'ui-icon-clock' } });
		$("#reportEntryLink").button({ icons: { primary: 'ui-icon-alert'} });
		$("#manageTags").button({ icons: { primary: 'ui-icon-wrench' } });

		var loggedUserId = @UserContext.LoggedUserId;
		var urlMapper = new vdb.UrlMapper('@UrlMapper.HostAddress');
		var eventRepo = new vdb.repositories.ReleaseEventRepository(urlMapper);
		var userRepo = new vdb.repositories.UserRepository(urlMapper, loggedUserId);
		var latestComments = @ToJS(Model.LatestComments);
		var users = @ToJS(Model.UsersAttending);
		var tags = @ToJS(Model.Tags);
		var canDeleteAllComments = @ToJS(UserContext.HasPermission(PermissionToken.DeleteComments));
		var reportTypes = @ToJS(ReportUtils.GetReportTypes(Translate.EventReportTypeNames, EventReport.ReportTypesWithRequiredNotes));

		var vm = new vdb.viewModels.releaseEvents.ReleaseEventDetailsViewModel(urlMapper, eventRepo, userRepo, latestComments,
			reportTypes, loggedUserId, @Model.Id, @ToJS((int?)Model.EventAssociationType), users, tags, canDeleteAllComments);
		ko.applyBindings(vm);

		$(".artistLink").vdbArtistToolTip();

	});
</script>
}
